<template>
  <el-dialog :visible="roleDialog" title="选择角色" @close="closeWindow">
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in roleList " :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>

    </el-checkbox-group>
    <el-row type="flex" justify="end" align="center">
      <el-button type="primary" @click="ok">确认</el-button>
      <el-button @click="closeWindow">取消</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  props: {
    roleDialog: {
      type: Boolean,
      default: false
    },
    userId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleList: [],
      roleIds: []
    }
  },
  methods: {
    async  getRoleList() {
      const { rows } = await getRoleList()
      this.roleList = rows
      console.log(this.roleList)
    },
    async getUserDetailById() {
      const { roleIds } = await getUserDetailById(this.userId)
      this.roleIds = roleIds
      console.log(roleIds, '用户的角色')
    },
    // 分配角色
    async ok() {
      await assignRoles({ id: this.userId, roleIds: this.roleIds })
      this.$message.success('保存成功')
      this.$emit('update:roleDialog', false)
    },
    closeWindow() {
      this.roleIds = []
      this.$emit('update:roleDialog', false)
    }
  }
}
</script>

<style>

</style>
